package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.servicebase.exceptionhandler.SelfExceptionHandler;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Component
public class VodServiceImpl implements VodService{

    @Value("${aliyun.oss.file.keyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.file.keySecret}")
    private  String accessKeySecret;


    /**
     * 上传视频
     * @param file
     * @return
     */
    @Override
    public String uploadVideo(MultipartFile file) {

        try {
            //accessKeyId, accessKeySecret
            //fileName：上传文件原始名称
            // 01.03.09.mp4
            String fileName = file.getOriginalFilename();
            //title：上传之后显示名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream：上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret,title, fileName, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return videoId;
        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据视频id删除阿里云视频
     * @param videoId
     * @return
     */
    @Override
    public boolean removeVideo(String videoId) {
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(accessKeyId, accessKeySecret);
            DeleteVideoResponse response = new DeleteVideoResponse();
            DeleteVideoRequest request = new DeleteVideoRequest();
            //videoId可以存放多个
            request.setVideoIds(videoId);
            client.getAcsResponse(request);
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
        }
       return  false;
    }

    /**
     * 删除课程或者章节的时候，需要批量删除视频
     * @param list
     * @return
     */
    @Override
    public boolean removeVideoBatch(List<String> list) {

        try {
            DefaultAcsClient client = InitVodClient.initVodClient(accessKeyId, accessKeySecret);
            DeleteVideoResponse response = new DeleteVideoResponse();
            DeleteVideoRequest request = new DeleteVideoRequest();
            //videoId可以存放多个
            request.setVideoIds(StringUtils.join(list.toArray(),","));
            client.getAcsResponse(request);
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  false;
    }
}
